/*
 * 
 * 小O首页
 * 
 * */
package com.shanghaionstar.mro;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.DealerBrandType;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.FinalConstants;


public class MrOHomeFragment extends MrOBaseFragment {
    private View view;
    // private Animation bodyRotateAnimation;
    // private Animation eyeRotateAnimation;
    // private Button first, second, third;
    // private ImageView iv_alpha, iv_fx;
    // private Boolean isBegin, isReduce;HomeHomeHome
    // private Round round;
    // private FrameLayout fl_loading;
    // FenbeiWave fenbeiWave;
    // private ArrayList<Float> volumes = new ArrayList<Float>();
    // private int volumesCount;
    private MrOActivity mroActivity;
    @FindViewById(background = R.drawable.mro_background_line)
    private CustomImageView background_line;
    @FindViewById
    private FrameLayout mro_default;
    @FindViewById(background = R.drawable.mro_figru)
    private CustomImageView mro_figue;
    @FindViewById(background = R.drawable.mro_talk)
    private CustomImageView mro_feedback;
    @FindViewById
    private CustomImageView mro_bigbody, mro_bighead;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.mro_home, container, false);
            injectView(view);
        } else {
            super.onResumeView(view);
        }
        initView();
        /*
         * view.findViewById(R.id.first).setOnClickListener(new
		 * OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { // TODO Auto-generated method
		 * stub remoteControl(); } });
		 * 
		 * view.findViewById(R.id.second).setOnClickListener(new
		 * OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { // TODO Auto-generated method
		 * stub mroActivity.changeFragment(mroActivity.mFragmentManager, new
		 * MrOPoiFragment(), MrOPoiFragment.class, true); } });
		 */
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        mroActivity = (MrOActivity) getActivity();
        checkFigureConfig();
    }

    private void checkFigureConfig() {
        String currentFigure = PreferencesUtls.getInstance(mroActivity).getString(FinalConstants.MroConstants.MRO_FIGURE_KEY);
        if ("".equals(currentFigure) || FinalConstants.MroConstants.MRO_FIGURE_DEFAULT.equals(currentFigure)) {
            mro_bigbody.setImageResource(R.drawable.mro_body_normal);
            mro_bighead.setImageResource(R.drawable.mro_head);
        } else if (FinalConstants.MroConstants.MRO_FIGURE_BRAND.equals(currentFigure)) {
            DealerBrandType myBrand = DealerBrandType.getDealerBrandTypeByDesc(ApplicationUtil.getInstance().getCarInfo());
            switch (myBrand) {
                case CHEVROLET:
                    mro_bigbody.setImageResource(R.drawable.mro_body_normal_chevrolet);
                    break;
                case BUICK:
                    mro_bigbody.setImageResource(R.drawable.mro_body_normal_buick);
                    break;
                case CADILLAC:
                    mro_bigbody.setImageResource(R.drawable.mro_body_normal_cadillac);
                    break;
            }
            mro_bighead.setImageResource(R.drawable.mro_head);
        } else if (FinalConstants.MroConstants.MRO_FIGURE_WTK.equals(currentFigure)) {
            mro_bigbody.setImageResource(R.drawable.mro_body_normal);
            mro_bighead.setImageResource(R.drawable.mro_head_wtk);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        checkPermission();
    }

    /**
     * 初始化
     */
    @com.onstar.cn.framework.annotation.OnClick({R.id.mro_figue, R.id.mro_default, R.id.mro_feedback})
    public void OnClick(View v) {
        // 点击形象按钮跳到选择形象页面
        if (v.getId() == R.id.mro_figue) {
            mroActivity.changeFragment(mroActivity.fragmentManager, new
                    MrOChooseFigueFragment(), MrOChooseFigueFragment.class, true);
        } else if (v.getId() == R.id.mro_default) {
            mroActivity.listen();
        } else if (v.getId() == R.id.mro_feedback) {
            mroActivity.listen(true);
        }
    }

    private void initView() {
        // 点击形象按钮跳到选择形象页面
        /*
		 * mro_figue.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { // TODO Auto-generated method
		 * stub // mroActivity.changeFragment(mroActivity.mFragmentManager, new
		 * // MrOChooseFigueFragment(), MrOChooseFigueFragment.class,true); }
		 * }); mro_default.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { // TODO Auto-generated method
		 * stub mroActivity.listen(); } });
		 */
        // round = new Round(this);
        // mLock = new Object();
        // int width = getWindowManager().getDefaultDisplay().getWidth();
        // int height = getWindowManager().getDefaultDisplay().getHeight();
        // Toast.makeText(this, width+"-------"+height, 1).show();
        // fenbeiWave = (FenbeiWave) findViewById(R.id.fenbei);
        // fl_loading = (FrameLayout) findViewById(R.id.fl_loading);
        // iv_fx = (ImageView) findViewById(R.id.iv_fx);
        // iv_alpha = (ImageView) findViewById(R.id.iv_alpha);
        // first = (Button) findViewById(R.id.first);
        // second = (Button) findViewById(R.id.second);
        // third = (Button) findViewById(R.id.third);
        // round = (Round) findViewById(R.id.round);
        // round.setVisibility(View.INVISIBLE);
        // first.setOnClickListener(this);
        // second.setOnClickListener(this);
        // third.setOnClickListener(this);
        // second.setClickable(false);
        // third.setClickable(false);
        // isBegin = false;
        // isReduce = false;
        // round.setOnClickListene(this);
    }

    // crash 5.0
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        view = null;
    }

    private void checkPermission() {
        if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            requestContactsPermissions();
        }
    }

    private void requestContactsPermissions() {
        // BEGIN_INCLUDE(contacts_permission_request)
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                Manifest.permission.RECORD_AUDIO)) {
            Log.e("shouldShow", "true");
            // Display a SnackBar with an explanation and a button to trigger the request.
            Snackbar.make(view, getString(R.string.mro_need_record_permission),
                    Snackbar.LENGTH_INDEFINITE)
                    .setAction(getString(R.string.mro_open), new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ActivityCompat
                                    .requestPermissions(getActivity(), new String[]{Manifest.permission.RECORD_AUDIO}, 1);
                        }
                    })
                    .show();
        } else {
            // Contact permissions have not been granted yet. Request them directly.
            Log.e("shouldShow", "false");
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.RECORD_AUDIO}, 1);
        }
    }

}
